package Hot100.Medium.GreedyAndDP.Greedy;

/**
 * 盛水最多的容器
 */
public class LC11 {
    public static void main(String[] args) {
        int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        LC11 solution = new LC11();
        System.out.println(solution.maxArea(height));
    }

    /**
     * 暴力双for循环法
     * @param height
     * @return
     */
    public int maxArea1(int[] height){
        int n = height.length;
        int res = 0;
        int left = 0;
        while(left < n){
            for(int right = left + 1; right < n; right++){
                res = Math.max(res,calArea(height[left], height[right], right -left ));
            }
            left++;
        }
        return res;
    }

    public int calArea(int left, int right, int len){
        return len * Math.min(left,right);
    }

    /**
     * 双指针优化版本
     */
    public int maxArea(int[] height){
        int left = 0;
        int right = height.length - 1;
        int res = 0;
        while(left < right){
            if(height[left] < height[right]){
                res = Math.max(res, (right - left) * height[left]);
                left++;
            } else {
                res = Math.max(res, (right - left) * height[right]);
                right--;
            }
        }
        return res;
    }

}
